package com.zr.repertory.inventorySettings.service.impl;

import com.zr.inventorySettings.mapper.InventoryMapper;
import com.zr.inventorySettings.pojo.*;
import com.zr.inventorySettings.service.InventoryService;
import com.zr.util.AllRecords;
import com.zr.util.ResultBuilderVo;
import com.zr.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by Calm on 2019/11/20.
 */
@Service
public class InventoryServiceImpl  implements InventoryService{
    @Autowired
    private InventoryMapper inventoryMapper;

                                                /*
                                                    库房标示部分
                                                */
    /*库房标示新增前验证*/
    public ResultVo vlidateAdd(WarehousemarkAddVo warehousemarkAddVo) {
        //验证库房标示编码不能重复
        int countCode =  inventoryMapper.Warehousemarkcode(warehousemarkAddVo.getWarehousemarkcode());
        if (countCode>0){
            return ResultBuilderVo.error("库房标示编码已存在,请重新输入");
        }
        //验证库房标示名称不能重复
        int countName =  inventoryMapper.queryWarehousemarkname(warehousemarkAddVo.getWarehousemarkname());
        if (countName>0){
            return ResultBuilderVo.error("库房标示名称已存在,请重新输入");
        }
        return ResultBuilderVo.success();
    }

    /*库房标示新增*/
    @Override
    @Transactional
    public ResultVo identificationAdd(WarehousemarkAddVo warehousemarkAddVo) {
        //验证库房标示编码和名称
        ResultVo resultVo = vlidateAdd(warehousemarkAddVo);
        if (!resultVo.getSuccess()){
            return resultVo;
        }

        Warehousemark warehousemark = new Warehousemark();
        BeanUtils.copyProperties(warehousemarkAddVo,warehousemark);
        warehousemark.setCreatename("小明");
        warehousemark.setCreatetime(new Date());
        warehousemark.setUpdatename("小明");
        warehousemark.setUpdatetime(new Date());
        inventoryMapper.identificationAdd(warehousemark);

        return ResultBuilderVo.success();
    }

    /*库房标识查询接口*/
    @Override
    public ResultVo identificationQuery(WarehousemarkSelectVo warehousemarkSelectVo) {
        //根据条件查询数量
        int countIdentificationQuery =  inventoryMapper.queryIdentificationQueryCount(warehousemarkSelectVo);
        //根据条件查询数据
        List<Warehousemark> warehousemarkList = inventoryMapper.queryIdentificationQueryData(warehousemarkSelectVo);
        //把0，1转换成汉字
        for (Warehousemark warehousemark:warehousemarkList){
            if (!warehousemark.getWarehousemarkstatus()){
                warehousemark.setWarehousemarkstatusName("禁用");
            }
            warehousemark.setWarehousemarkstatusName("启用");
        }
        //把数据包装进allRecords
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(warehousemarkSelectVo.getPageIndex());
        allRecords.setPageSize(warehousemarkSelectVo.getPageSize());
        allRecords.resetTotalPage(countIdentificationQuery);
        allRecords.setTotalNumber(countIdentificationQuery);
        allRecords.setDataList(warehousemarkList);
        return ResultBuilderVo.success(allRecords);
    }

    //库房标识修改前验证
    private ResultVo vlidateIdentificationUpdate(WarehousemarkUpdateVo warehousemarkUpdateVo){
        //验证是否存在要修改的库房标识
        Warehousemark warehousemark = inventoryMapper.queryIdentificationById(warehousemarkUpdateVo.getWarehousemarkid());
        if(warehousemark==null){
            return  ResultBuilderVo.error("500","要修改的库房标示数据不存在");
        }
        //验证是否为禁用状态
        if(warehousemarkUpdateVo.getWarehousemarkstatus()){
            return  ResultBuilderVo.error("500","禁用状态下才能修改");
        }
        //验证库房标识名称是否存在
        Warehousemark warehousemark2=inventoryMapper.queryIdentificationByName(warehousemarkUpdateVo.getWarehousemarkname());
        //根据id判断是否修改的数据和查询出数据的id一致
        // 如果一致代表修改的还是原来的名称不用提示错误
        //否则提示名称已经存在！
        if (warehousemark2.getWarehousemarkid()!= warehousemarkUpdateVo.getWarehousemarkid()){
            return ResultBuilderVo.error("500","库房标示名称已存在");
        }
        return ResultBuilderVo.success();
    }

    /*库房标识修改接口*/
    @Override
    @Transactional
    public ResultVo identificationUpdate(WarehousemarkUpdateVo warehousemarkUpdateVo) {
        //库房标识修改前验证
        ResultVo resultVo = vlidateIdentificationUpdate(warehousemarkUpdateVo);
        if (!resultVo.getSuccess()){
            return resultVo;
        }
        //给修改人和修改时间等参数赋值
        //把左边对象中的参数值自动赋值给右边对象中的参数值
        //注意必须是相同类型和相同参数名的才能copy
        Warehousemark warehousemark=new Warehousemark();
        BeanUtils.copyProperties(warehousemarkUpdateVo,warehousemark);
        warehousemark.setUpdatename("小明");
        warehousemark.setUpdatetime(new Date());
        //修改语句。
        int update = inventoryMapper.identificationUpdate(warehousemark);
        return ResultBuilderVo.success();
    }

    /*库房标识启用/禁用接口*/
    @Override
    @Transactional
    public ResultVo identificationUpdateStatus(WarehousemarkUpdateStatusVo warehousemarkUpdateStatusVo) {
        Warehousemark warehousemark = inventoryMapper.queryIdentificationById(warehousemarkUpdateStatusVo.getId());
        if(warehousemark==null){
            return  ResultBuilderVo.error("500","要修改的库房标示数据不存在");
        }
        //给修改人员和修改时间赋值
        warehousemark.setWarehousemarkstatus(warehousemarkUpdateStatusVo.getStatus());
        warehousemark.setUpdatename("小明");
        warehousemark.setUpdatetime(new Date());
        inventoryMapper.identificationUpdateStatus(warehousemark);
        return ResultBuilderVo.success();
    }

                                        /*
                                            库房部分
                                        */
    /*新增前验证*/
    public ResultVo vlidateRoomAdd(WarehouseAddVo warehouseAddVo) {
        //验证库房标示编码不能重复
        int countCode =  inventoryMapper.Warehousecode(warehouseAddVo.getWarehousecode());
        if (countCode>0){
            return ResultBuilderVo.error("库房编码已存在,请重新输入");
        }
        //验证库房标示名称不能重复
        int countName =  inventoryMapper.queryWarehousename(warehouseAddVo.getWarehousename());
        if (countName>0){
            return ResultBuilderVo.error("库房名称已存在,请重新输入");
        }
        return ResultBuilderVo.success();
    }

    @Override
    @Transactional
    /*库房新增接口*/
    public ResultVo roomAdd(WarehouseAddVo warehouseAddVo) {

        //验证库房标示编码和名称
        ResultVo resultVo = vlidateRoomAdd(warehouseAddVo);
        if (!resultVo.getSuccess()){
            return resultVo;
        }
        Warehouse warehouse = new Warehouse();
        BeanUtils.copyProperties(warehouseAddVo,warehouse);
        warehouse.setCreatename("张三");
        warehouse.setCreatetime(new Date());
        warehouse.setUpdatename("李四");
        warehouse.setUpdatetime(new Date());
        inventoryMapper.roomAdd(warehouse);
        return ResultBuilderVo.success();
    }

    /*库房分页查询接口*/
    @Override
    public ResultVo roomQuerypage(WarehouseSelectVo warehouseSelectVo) {
        //根据条件查询数量
        int countRoomQuerypage =  inventoryMapper.queryRoomQuerypageCount(warehouseSelectVo);
        //根据条件查询数据
        List<Warehouse> warehouseList = inventoryMapper.queryRoomQuerypageData(warehouseSelectVo);
        //把0，1转换成汉字
        for (Warehouse warehouse:warehouseList){
            if (!warehouse.getWarehousestatus()){
                warehouse.setWarehousestatusname("禁用");
            }
            warehouse.setWarehousestatusname("启用");
        }
        //把数据包装进allRecords
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(warehouseSelectVo.getPageIndex());
        allRecords.setPageSize(warehouseSelectVo.getPageSize());
        allRecords.resetTotalPage(countRoomQuerypage);
        allRecords.setTotalNumber(countRoomQuerypage);
        allRecords.setDataList(warehouseList);
        return ResultBuilderVo.success(allRecords);
    }

    /*修改前验证*/
    public ResultVo vlidateRoomUpdateUpdate(WarehouseUpdateVo warehouseUpdateVo) {
        //验证是否存在要修改的库房
        Warehouse warehouse = inventoryMapper.queryRoomById(warehouseUpdateVo.getWarehouseid());
        if(warehouse==null){
            return  ResultBuilderVo.error("500","要修改的库房数据不存在");
        }
        //验证是否为禁用状态
        if(warehouseUpdateVo.getWarehousestatus()){
            return  ResultBuilderVo.error("500","禁用状态下才能修改");
        }
        //验证库房名称是否存在
        Warehouse warehouse2=inventoryMapper.queryRoomByName(warehouseUpdateVo.getWarehousename());
        //根据id判断是否修改的数据和查询出数据的id一致
        // 如果一致代表修改的还是原来的名称不用提示错误
        //否则提示名称已经存在！
        if (warehouse2.getWarehouseid()!= warehouseUpdateVo.getWarehouseid()){
            return ResultBuilderVo.error("500","库房名称已存在");
        }
        return ResultBuilderVo.success();
    }
    /*库房修改接口*/
    @Override
    @Transactional
    public ResultVo roomUpdate(WarehouseUpdateVo warehouseUpdateVo) {
        //库房标识修改前验证
        ResultVo resultVo = vlidateRoomUpdateUpdate(warehouseUpdateVo);
        if (!resultVo.getSuccess()){
            return resultVo;
        }
        //给修改人和修改时间等参数赋值
        //把左边对象中的参数值自动赋值给右边对象中的参数值
        //注意必须是相同类型和相同参数名的才能copy
        Warehouse warehouse=new Warehouse();
        BeanUtils.copyProperties(warehouseUpdateVo,warehouse);
        warehouse.setUpdatename("小刚");
        warehouse.setUpdatetime(new Date());
        //修改语句。
        int update = inventoryMapper.roomUpdate(warehouse);
        return ResultBuilderVo.success();
    }

    /*库房启用/禁用接口*/
    @Override
    @Transactional
    public ResultVo roomUpdateStatus(WarehousemarkUpdateStatusVo warehousemarkUpdateStatusVo) {
        Warehouse warehouse = inventoryMapper.queryRoomById(warehousemarkUpdateStatusVo.getId());
        if(warehouse==null){
            return  ResultBuilderVo.error("500","要修改的库房数据不存在");
        }
        //给修改人员和修改时间赋值
        warehouse.setWarehousestatus(warehousemarkUpdateStatusVo.getStatus());
        warehouse.setUpdatename("小明");
        warehouse.setUpdatetime(new Date());
        inventoryMapper.roomUpdateStatus(warehouse);
        return ResultBuilderVo.success();
    }

                                                /*
                                                    库区属性部分
                                         */
     /*库区属性新增前验证*/
    public ResultVo vlidateAttributeAdd(ReservoirareapropertiesAddVo reservoirareapropertiesAddVo) {
        //验证库区属性编码不能重复
        int countCode =  inventoryMapper.Attributecode(reservoirareapropertiesAddVo.getReservoirpropertiescode());
        if (countCode>0){
            return ResultBuilderVo.error("库区编码已存在,请重新输入");
        }
        //验证库区属性名称不能重复
        int countName =  inventoryMapper.Attributename(reservoirareapropertiesAddVo.getReservoirpropertiesname());
        if (countName>0){
            return ResultBuilderVo.error("库区名称已存在,请重新输入");
        }
        return ResultBuilderVo.success();
    }

    /*库区属性新增*/
    @Override
    @Transactional
    public ResultVo attributeAdd(ReservoirareapropertiesAddVo reservoirareapropertiesAddVo) {
        //验证库区属性编码和名称
        ResultVo resultVo = vlidateAttributeAdd(reservoirareapropertiesAddVo);
        if (!resultVo.getSuccess()){
            return resultVo;
        }
        inventoryMapper.AttributeAdd(reservoirareapropertiesAddVo);
        return ResultBuilderVo.success();
    }

    /*库区属性分页*/
    @Override
    public ResultVo attributeQuery(ReservoirareapropertiesSelectVo reservoirareapropertiesSelectVo) {
        //根据条件查询数量
        int countAttributeQuerypage =  inventoryMapper.queryAttributeQuerypageCount(reservoirareapropertiesSelectVo);
        //根据条件查询数据
        List<Reservoirareaproperties> reservoirareapropertiesList = inventoryMapper.queryAttributeQuerypageData(reservoirareapropertiesSelectVo);
        //把0，1转换成汉字
        for (Reservoirareaproperties reservoirareaproperties:reservoirareapropertiesList){
            if (!reservoirareaproperties.getReservoirpropertiesstatus()){
                reservoirareaproperties.setReservoirpropertiesstatusname("禁用");
            }
            reservoirareaproperties.setReservoirpropertiesstatusname("启用");
        }
        //把数据包装进allRecords
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(reservoirareapropertiesSelectVo.getPageIndex());
        allRecords.setPageSize(reservoirareapropertiesSelectVo.getPageSize());
        allRecords.resetTotalPage(countAttributeQuerypage);
        allRecords.setTotalNumber(countAttributeQuerypage);
        allRecords.setDataList(reservoirareapropertiesList);
        return ResultBuilderVo.success(allRecords);
    }

    /*库区属性修改前验证*/
    public ResultVo vlidateAttributeUpdate(ReservoirareapropertiesUpdateVo reservoirareapropertiesUpdateVo) {
        //验证是否存在要修改的库区属性
        Reservoirareaproperties reservoirareaproperties = inventoryMapper.queryAttributeById(reservoirareapropertiesUpdateVo.getReservoirpropertiesid());
        if(reservoirareaproperties==null){
            return  ResultBuilderVo.error("500","要修改的库区属性数据不存在");
        }
        //验证是否为禁用状态
        if(reservoirareapropertiesUpdateVo.getReservoirpropertiesstatus()){
            return  ResultBuilderVo.error("500","禁用状态下才能修改");
        }
        //验证库区属性名称是否存在
        Reservoirareaproperties reservoirareaproperties1=inventoryMapper.queryAttributeByName(reservoirareapropertiesUpdateVo.getReservoirpropertiesname());
        //根据id判断是否修改的数据和查询出数据的id一致
        // 如果一致代表修改的还是原来的名称不用提示错误
        //否则提示名称已经存在！
        if (reservoirareaproperties1.getReservoirpropertiesid()!= reservoirareapropertiesUpdateVo.getReservoirpropertiesid()){
            return ResultBuilderVo.error("500","库区属性名称已存在");
        }
        return ResultBuilderVo.success();
    }

    /*库区属性修改接口*/
    @Override
    @Transactional
    public ResultVo attributeUpdate(ReservoirareapropertiesUpdateVo reservoirareapropertiesUpdateVo) {
        //库区属性修改前验证
        ResultVo resultVo = vlidateAttributeUpdate(reservoirareapropertiesUpdateVo);
        if (!resultVo.getSuccess()){
            return resultVo;
        }
        //给修改人和修改时间等参数赋值
        //把左边对象中的参数值自动赋值给右边对象中的参数值
        //注意必须是相同类型和相同参数名的才能copy
        //修改语句。
        inventoryMapper.attributeUpdate(reservoirareapropertiesUpdateVo);
        return ResultBuilderVo.success();
    }

    /*库区属性启用/禁用接口*/
    @Override
    @Transactional
    public ResultVo attributeUpdateStatus(WarehousemarkUpdateStatusVo warehousemarkUpdateStatusVo) {
        Reservoirareaproperties reservoirareaproperties = inventoryMapper.queryAttributeById(warehousemarkUpdateStatusVo.getId());
        if(reservoirareaproperties==null){
            return  ResultBuilderVo.error("500","要修改的库区属性数据不存在");
        }
        //给修改人员和修改时间赋值
        reservoirareaproperties.setReservoirpropertiesstatus(warehousemarkUpdateStatusVo.getStatus());
        inventoryMapper.attributeUpdateStatus(reservoirareaproperties);
        return ResultBuilderVo.success();
    }

                                                    /*
                                                        库区部分
                                                    */
    /*库房编码下拉框接口*/
    @Override
    public ResultVo roomXiala() {
        List<Warehouse> warehouseList = inventoryMapper.roomXiala();
        return ResultBuilderVo.success(warehouseList);
    }

    /*库区新增前验证*/
    public ResultVo vlidateRoomareaAdd(ReservoirareaAddVo reservoirareaAddVo) {
        //验证库房编码是否存在
        int countWarehouseCode = inventoryMapper.Warehousecode(reservoirareaAddVo.getWarehousecode());
        if (countWarehouseCode<0){
            return ResultBuilderVo.error("库房编码不存在,请重新选择");
        }

        //验证库区编码不能重复
        int countCode =  inventoryMapper.Roomareacode(reservoirareaAddVo.getReservoirareacode());
        if (countCode>0){
            return ResultBuilderVo.error("库区编码已存在,请重新输入");
        }
        //验证库区名称不能重复
        int countName =  inventoryMapper.Roomareaname(reservoirareaAddVo.getReservoirareaname());
        if (countName>0){
            return ResultBuilderVo.error("库区名称已存在,请重新输入");
        }
        return ResultBuilderVo.success();

    }

    /*新增库区接口*/
    @Override
    @Transactional
    public ResultVo roomareaAdd(ReservoirareaAddVo reservoirareaAddVo) {
        //验证库房编码是否存在
        //验证库区的编码和名称是否重复
        ResultVo resultVo = vlidateRoomareaAdd(reservoirareaAddVo);
        if (!resultVo.getSuccess()){
            return resultVo;
        }
        //将库区表中的内容添加
        inventoryMapper.roomareaAdd(reservoirareaAddVo);
        //将库区属性表和库区中间表内容添加入库
        inventoryMapper.roomareaAdd2(reservoirareaAddVo);
        return ResultBuilderVo.success();
    }

    /*库区分页接口*/
    @Override
    public ResultVo roomareaQuerypage(ReservoirareaSelectVo reservoirareaSelectVo) {
        //根据条件查询数量
        int countRoomareaQuery =  inventoryMapper.queryRoomareaQueryCount(reservoirareaSelectVo);
        //根据条件查询数据
        List<Reservoirarea> reservoirareaList = inventoryMapper.queryRoomareaQueryData(reservoirareaSelectVo);
        //把0，1转换成汉字
        for (Reservoirarea reservoirarea:reservoirareaList){
            if (!reservoirarea.getReservoirareastatus()){
                reservoirarea.setReservoirareastatusname("禁用");
            }
            reservoirarea.setReservoirareastatusname("启用");
        }
        //把数据包装进allRecords
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(reservoirareaSelectVo.getPageIndex());
        allRecords.setPageSize(reservoirareaSelectVo.getPageSize());
        allRecords.resetTotalPage(countRoomareaQuery);
        allRecords.setTotalNumber(countRoomareaQuery);
        allRecords.setDataList(reservoirareaList);
        return ResultBuilderVo.success(allRecords);
    }

    /*库区修改前验证*/
    public ResultVo vlidateRoomareaUpdate(ReservoirareaUpdateVo reservoirareaUpdateVo) {
        //验证库房编码是否存在
        int countWarehouseCode = inventoryMapper.Warehousecode(reservoirareaUpdateVo.getWarehousecode());
        if (countWarehouseCode<0){
            return ResultBuilderVo.error("要修改的库房编码不存在,请选择后修改");
        }
        //验证是否存在要修改的库区
        Reservoirarea reservoirarea = inventoryMapper.queryRoomareaById(reservoirareaUpdateVo.getReservoirareaid());
        if(reservoirarea==null){
            return  ResultBuilderVo.error("500","要修改的库区数据不存在");
        }
        //验证是否为禁用状态
        if(reservoirareaUpdateVo.getReservoirareastatus()){
            return  ResultBuilderVo.error("500","禁用状态下才能修改");
        }
        //验证库区名称是否存在
        Reservoirarea reservoirarea1=inventoryMapper.queryRoomareaByName(reservoirareaUpdateVo.getReservoirareaname());
        //根据id判断是否修改的数据和查询出数据的id一致
        // 如果一致代表修改的还是原来的名称不用提示错误
        //否则提示名称已经存在！
        if (reservoirarea1.getReservoirareaid()!= reservoirareaUpdateVo.getReservoirareaid()){
            return ResultBuilderVo.error("500","库区名称已存在");
        }
        return ResultBuilderVo.success();
    }

    /*库区修改接口*/
    @Override
    @Transactional
    public ResultVo roomareaUpdate(ReservoirareaUpdateVo reservoirareaUpdateVo) {
        //库区修改前验证
        ResultVo resultVo = vlidateRoomareaUpdate(reservoirareaUpdateVo);
        if (!resultVo.getSuccess()){
            return resultVo;
        }
        //给修改人和修改时间等参数赋值
        //把左边对象中的参数值自动赋值给右边对象中的参数值
        //注意必须是相同类型和相同参数名的才能copy
        //修改库区表
        inventoryMapper.roomareaUpdate(reservoirareaUpdateVo);
        //修改库区属性表和库区中间表
        //修改前删除中间表的旧数据
        inventoryMapper.delRoomarea2(reservoirareaUpdateVo);
        inventoryMapper.roomareaUpdate2(reservoirareaUpdateVo);
        return ResultBuilderVo.success();
    }

    /*库区查看接口*/
    @Override
    public ResultVo roomareaSee(ReservoirareaSeeVo reservoirareaSeeVo) {
        //验证是否存在要修改的库区
        Reservoirarea reservoirarea = inventoryMapper.queryRoomareaById(reservoirareaSeeVo.getReservoirareaid());
        if(reservoirarea==null){
            return  ResultBuilderVo.error("500","要修改的库区数据不存在");
        }
        List<Reservoirareaproperties> list = inventoryMapper.queryRoomareaProperties(reservoirareaSeeVo.getReservoirareaid());
        BeanUtils.copyProperties(reservoirareaSeeVo,reservoirarea);
        reservoirarea.setReservoirareapropertiesList(list);
        return ResultBuilderVo.success(reservoirarea);
    }

    /* 库区启用/禁用接口*/
    @Override
    @Transactional
    public ResultVo roomareaUpdateStatus(WarehousemarkUpdateStatusVo warehousemarkUpdateStatusVo) {
        //验证是否存在要修改的库区
        Reservoirarea reservoirarea = inventoryMapper.queryRoomareaById(warehousemarkUpdateStatusVo.getId());
        if(reservoirarea==null){
            return  ResultBuilderVo.error("500","要修改的库区数据不存在");
        }
        //给修改人员和修改时间赋值
        reservoirarea.setReservoirareastatus(warehousemarkUpdateStatusVo.getStatus());
        inventoryMapper.roomareaUpdateStatus(reservoirarea);
        return ResultBuilderVo.success();
    }

                                                /*
                                                    库位部分
                                                */
    /*库区编码下拉框接口*/
    @Override
    public ResultVo roomSpaceXiala(Integer warehouseid) {
        List<Reservoirarea> ReservoirareaList = inventoryMapper.queryByWarehouseId(warehouseid);
        return ResultBuilderVo.success(ReservoirareaList);
    }

    /*根据库区编码展示原有的库位信息*/
    @Override
    public ResultVo roomSpaceKuwei(Integer reservoirareaid) {
        List<Storagelocation> storagelocationList = inventoryMapper.queryByReservoirareaId(reservoirareaid);
        return  ResultBuilderVo.success(storagelocationList);
    }

    /*库位新增前验证*/
    public ResultVo vlidateRoomSpaceAdd(StoragelocationAddVo storagelocationAddVo) {
        //验证库区是否存在
        int countReservoirareaCode = inventoryMapper.reservoirareaBycode(storagelocationAddVo.getReservoirareaid());
        if (countReservoirareaCode<0){
            return ResultBuilderVo.error("库区编码不存在,请重新选择");
        }

        //验证库位编码是否重复
        int countCode =  inventoryMapper.RoomSpaceecode(storagelocationAddVo.getStoragelocationcode());
        if (countCode>0){
            return ResultBuilderVo.error("库位编码已存在,请重新输入");
        }
        //验证库位名称是否重复
        int countName =  inventoryMapper.RoomSpacename(storagelocationAddVo.getStoragelocationname());
        if (countName>0){
            return ResultBuilderVo.error("库位名称已存在,请重新输入");
        }
        return ResultBuilderVo.success();
    }

    /*库位新增接口*/
    @Override
    @Transactional
    public ResultVo roomSpaceAdd(StoragelocationAddVo storagelocationAddVo) {
        //验证库区的编码是否存在
        //验证库位编码是否重复
        //验证库位名称是否重复
        ResultVo resultVo = vlidateRoomSpaceAdd(storagelocationAddVo);
        if (!resultVo.getSuccess()){
            return resultVo;
        }
        //添加前删除库位中原有的内容
        inventoryMapper.roomSpaceDel(storagelocationAddVo);
        //将库位表中的新内容入库
        inventoryMapper.roomSpaceAdd(storagelocationAddVo);
        return ResultBuilderVo.success();
    }

    /*库位分页查询接口*/
    @Override
    public ResultVo roomSpaceQuerypage(StoragelocationSelectVo storagelocationSelectVo) {
        //根据条件查询数量
        int countRoomSpaceQuery =  inventoryMapper.queryRoomSpaceQueryCount(storagelocationSelectVo);
        //根据条件查询数据
        List<Storagelocation> storagelocationList = inventoryMapper.queryRoomSpaceQueryData(storagelocationSelectVo);
        //把0，1转换成汉字
        for (Storagelocation storagelocation:storagelocationList){
            if (!storagelocation.getStoragelocationstatus()){
                storagelocation.setStoragelocationstatusname("禁用");
            }
            storagelocation.setStoragelocationstatusname("启用");
        }
        //把数据包装进allRecords
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(storagelocationSelectVo.getPageIndex());
        allRecords.setPageSize(storagelocationSelectVo.getPageSize());
        allRecords.resetTotalPage(countRoomSpaceQuery);
        allRecords.setTotalNumber(countRoomSpaceQuery);
        allRecords.setDataList(storagelocationList);
        return ResultBuilderVo.success(allRecords);
    }

    /*库位修改前验证*/
    private ResultVo vlidateRoomSpaceUpdate(StoragelocationUpdateVo storagelocationUpdateVo) {
        //验证是否为禁用状态
        if(storagelocationUpdateVo.getStoragelocationstatus()){
            return  ResultBuilderVo.error("500","禁用状态下才能修改");
        }
        //验证库位名称是否存在
        Storagelocation storagelocation = inventoryMapper.queryRoomSpaceByName(storagelocationUpdateVo.getStoragelocationname());
        //根据id判断是否修改的数据和查询出数据的id一致
        // 如果一致代表修改的还是原来的名称不用提示错误
        //否则提示名称已经存在！
        if (storagelocation.getReservoirareaid()!= storagelocationUpdateVo.getStoragelocationid()){
            return ResultBuilderVo.error("500","库位名称已存在");
        }
        return ResultBuilderVo.success();
    }

    /*库位修改接口*/
    @Override
    @Transactional
    public ResultVo roomSpaceUpdate(StoragelocationUpdateVo storagelocationUpdateVo) {
        //库位修改前验证
        ResultVo resultVo = vlidateRoomSpaceUpdate(storagelocationUpdateVo);
        if (!resultVo.getSuccess()){
            return resultVo;
        }
        //给修改人和修改时间等参数赋值
        //把左边对象中的参数值自动赋值给右边对象中的参数值
        //注意必须是相同类型和相同参数名的才能copy
        //修改库区属性表和库区中间表
        inventoryMapper.roomSpaceUpdate(storagelocationUpdateVo);
        return ResultBuilderVo.success();
    }

    /* 库位启用/禁用接口*/
    @Override
    @Transactional
    public ResultVo roomspaceUpdateStatus(WarehousemarkUpdateStatusVo warehousemarkUpdateStatusVo) {
        //验证是否存在要修改的库位
        Storagelocation storagelocation = inventoryMapper.queryRoomSpaceById(warehousemarkUpdateStatusVo.getId());
        if(storagelocation==null){
            return  ResultBuilderVo.error("500","要修改的库位数据不存在");
        }
        //给修改人员和修改时间赋值
        storagelocation.setStoragelocationstatus(warehousemarkUpdateStatusVo.getStatus());
        inventoryMapper.roomSpaceUpdateStatus(storagelocation);
        return ResultBuilderVo.success();
    }
                                                /*
                                                    库房归属部分
                                                */
    /*库房归属分页查询接口*/
    @Override
    public ResultVo belongingsQuerypage(DeoptBelongVo deoptBelongVo) {
        //根据条件查询数量
        int countBelongingsQuery =  inventoryMapper.queryBelongQueryCount(deoptBelongVo);
        //根据条件查询数据
        List<DeoptBelong> deoptBelongList = inventoryMapper.queryBelongQueryData(deoptBelongVo);
        //把0，1转换成汉字
        for (DeoptBelong deoptBelong:deoptBelongList){
            if (!deoptBelong.getWareHouseStatus()){
                deoptBelong.setWareHouseStatusName("禁用");
            }
            deoptBelong.setWareHouseStatusName("启用");
        }
        //把数据包装进allRecords
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(deoptBelongVo.getPageIndex());
        allRecords.setPageSize(deoptBelongVo.getPageSize());
        allRecords.resetTotalPage(countBelongingsQuery);
        allRecords.setTotalNumber(countBelongingsQuery);
        allRecords.setDataList(deoptBelongList);
        return ResultBuilderVo.success(allRecords);
    }

    /*库房归属查看接口*/
    @Override
    public DeopBelongSeeVo see(Integer legalPersonId) {
        DeopBelongSeeVo deopBelongSeeVo = inventoryMapper.see(legalPersonId);
        return deopBelongSeeVo;
    }

    /*多条件查询库房的信息*/
    @Override
    public List<Warehouse> queryAllWareHouse(WarehouseSelectVo warehouseSelectVo) {

        return inventoryMapper.queryAllWareHouse(warehouseSelectVo);
    }

    /*根据勾选的库房信息查库区信息*/
    @Override
    public List<Reservoirarea> queryReservoirarea(Integer warehouseid) {
        return inventoryMapper.queryReservoirarea(warehouseid);
    }

    /*根据勾选的库区信息查库位信息*/
    @Override
    public List<Storagelocation> queryStoragelocation(Integer reservoirareaid) {
        return inventoryMapper.queryStoragelocation(reservoirareaid);
    }

    /*根据法人查对应的库位库区库房信息*/
    @Override
    public   DeopBelongSeeVo  queryStoragelocationAndLegalPerson(Integer legalPersonId) {
        return inventoryMapper.queryStoragelocationAndLegalPerson(legalPersonId);
    }

   /* *//*根据库位信息查对应的库区信息*//*
    @Override
    public List<Reservoirarea> queryReservoirareaAndLegalPerson(Integer storagelocationid) {
        return inventoryMapper.queryReservoirareaAndLegalPerson(storagelocationid);
    }

    *//*根据库区信息查对应的库房信息*//*
    @Override
    public List<Warehouse> queryWarehouseAndLegalPerson(Integer reservoirareaid){
        return inventoryMapper.queryWarehouseAndLegalPerson(reservoirareaid);
    }*/

    /*根据库区属性下拉框查库区信息*/
    @Override
    public List<Reservoirarea> queryReservoirPropertiesAndReservoir(Integer reservoirPropertiesId) {
        return inventoryMapper.queryReservoirPropertiesAndReservoir(reservoirPropertiesId);
    }

    /*库房归属新增前验证*/
    public ResultVo vlidatebelongingsAdd(Legalpersonandstoragelocation legalpersonandstoragelocation) {
        //验证法人是否存在
        int countReservoirareaCode = inventoryMapper.reserbelongingsById(legalpersonandstoragelocation.getLegalpersonid());
        if (countReservoirareaCode<0){
            return ResultBuilderVo.error("法人不存在,请选择");
        }
        return ResultBuilderVo.success();
    }

    /*库房归属新增接口*/
    @Override
    @Transactional
    public ResultVo belongingsAdd(Legalpersonandstoragelocation legalpersonandstoragelocation) {
        //验证法人是否存在
        ResultVo resultVo = vlidatebelongingsAdd(legalpersonandstoragelocation);
        if (!resultVo.getSuccess()){
            return resultVo;
        }
        //新增前删除原有的中间表信息
        inventoryMapper.delLegalpersonandstoragelocation(legalpersonandstoragelocation.getLegalpersonid());
        //重新添加数据到中间表中
        inventoryMapper.addLegalpersonandstoragelocation(legalpersonandstoragelocation);
        return ResultBuilderVo.success();
    }

    /*库房归属修改接口*/
    @Override
    @Transactional
    public ResultVo belongingsUpdate(Legalpersonandstoragelocation legalpersonandstoragelocation) {
        //验证法人是否存在
        ResultVo resultVo = vlidatebelongingsAdd(legalpersonandstoragelocation);
        if (!resultVo.getSuccess()){
            return resultVo;
        }
        //修改前删除原有的中间表信息
        inventoryMapper.delLegalpersonandstoragelocation(legalpersonandstoragelocation.getLegalpersonid());
        //重新添加数据到中间表中
        inventoryMapper.addLegalpersonandstoragelocation(legalpersonandstoragelocation);
        return ResultBuilderVo.success();
    }


}
